/**
 * 如果数据大小无法确定的话，可以采用链式存储实现 队列，避免出现空间的浪费
 */


class Node {
    constructor (data, next = null) {
        this.data = data
        this.next = next
    }
}

 /**
  * 可以直接在 线性表的基础上实现 队列，但是效率不高
  */
class QueueChain {
    constructor () {
        let node = new Node() // 头节点
        this.data = node
        this.head = this.rear = node
    }
    /**
     * 链表尾部插入数据
     * @param {*} val  插入值
     */
    push (val) {
        let status = 'success'
        let node = new Node(val)
        if (!node) {
            status = 'overflow'
        } else {
            this.rear.next = node
            this.rear = node
            status = 'success'
        }
        return status
    }
    shift () {
        let status = 'underflow'
        if (!this.empty()) {
            let node = this.head.next
            this.head.next = node.next
            if (node === this.rear) {
                this.rear = this.head
                node = null
            }
            status = 'success'
        }
        return status
    }
    /**
     * 取队列头部元素
     */
    top () {
        let status = 'underflow'
        if (!this.empty()) {
            console.log(this.head.next.data)
            status = 'success'
        }
        return status
    }
    empty () {
        return this.head.next === null
    }
}

const a = new QueueChain(10)
 a.push('d')
 a.push('t')
 a.push('x')
 a.push('d')
 a.push('t')
 a.push('x')
 a.push('d')
 a.push('t')
 a.push('x')
 a.push(3)
 a.push(2)
 a.push(1)
 console.log(JSON.stringify(a.data))
 a.shift()
 a.shift()
 console.log(JSON.stringify(a.data))
